

$(function(){
        var username = localStorage.getItem("parameter");
        var password = localStorage.getItem("password");
        var token = localStorage.getItem("token");
        $("#real-time").click(function(){
                earn_response_show_list_real()
        })
        function earn_response_show_list_real(){
                // var url = "http://101.33.204.176:8080/recommend/recommend/";
                var url = "http://127.0.0.1:8080/recommend/realtime/";
                $.ajax({
                        url:url,
                        type:"POST",
                        data:{
                                token:token
                        },
                        success: function (response){
                                // console.log(data);
                                // 这里编写一个函数 将后端返回的数据显示到前端
                                show_list_real(response);
                        }
                })
        }


        function show_list_real(response){
                // 获取data中 productId avg 使用each循环

                $.each(response.data.recs,function(index,value){
                        // 分别获取他的productId和avg的值
                        var productId = response.data.recs[index].productId;
                        var score = response.data.recs[index].score;
                        var div_li = '<div class="col-sm-6"><div class="thumbnail"><img id="imgurl-'+productId+'" src="..." alt="..."><div class="caption"><h3 id="goodsname-'+productId+'"></h3><p id="avg">'+'推荐值：'+score+'</p><p><a href="#" id="btn-'+productId+'" class="btn btn-primary" role="button">进入详情</a> </p></div></div></div>'
                        $(".container-inner").append(div_li);
                        earn_response_show_img_name_real(productId);
                        // console.log(productName);
                })

                
        }

        
        // 显示其他信息
        function earn_response_show_img_name_real(productId){
                // var url = "http://101.33.204.176:8080/product/data/";
                var url = "http://127.0.0.1:8080/product/data/";
                // return new Promise(function(resolve, reject) {
                        $.ajax({
                                url:url,
                                type:"GET",
                                data:{
                                        productId:productId
                                },
                                success: function (response){
                                        // console.log(response)
                                        var name = response.data.name;
                                        var imgurl = response.data.imageUrl;
                                        // console.log(imgurl);
                                        // console.log(index)
                                        // console.log($("#goodsname").eq(index).text())
                                        $("#goodsname-"+productId).text(name)
                                        $("#imgurl-"+productId).attr("src", imgurl)
                                        // resolve(name);
                                        
                                }
                        })
                // });
                        
        }

        $(document).on("click", ".btn-primary", function() {
                var h3Content = $(this).closest(".thumbnail").find("h3").text();
                var buttonId = $(this).attr("id");
                var ID = buttonId.substring(4);

                // 重定向到登陆界面 
                window.location.href = "./goodsdetails.html?h3Content="+h3Content+"&ID="+ID;
        });
})